package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;


    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.select(new ShoppingCart());
    }

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        //每个菜品和套餐添加都会触发添加事件
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //把用户id设置进去
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());

        //查询购物车，如果是空就直接新增，不是空就对比套餐id或菜品id，，如果都不是就直接新增
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() >0) {
            ShoppingCart lodShoppingCart = shoppingCartList.get(0);
            lodShoppingCart.setNumber(lodShoppingCart.getNumber() +1);
            lodShoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.update(lodShoppingCart);
        }else {
            if (dto.getSetmealId() != null) {
                Setmeal setmeal = setmealMapper.selectBySetmeaId(dto.getSetmealId());
                BeanUtils.copyProperties(setmeal, shoppingCart);
                shoppingCart.setAmount(setmeal.getPrice());
            } else if (dto.getDishId() != null) {
                Dish dish = dishMapper.selectByDishId(dto.getDishId());
                BeanUtils.copyProperties(dish, shoppingCart);
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 删除一个菜品或套餐
     * @param dto
     */
    @Override
    public void update(ShoppingCartDTO dto) {
        //先查询，如果这个菜品或者套餐的数量等于1就删除整个购物车
        ShoppingCart shoppingCartNew=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCartNew);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCartNew);
        ShoppingCart shoppingCart = shoppingCarts.get(0);
        Integer number = shoppingCart.getNumber();
        if ( number > 1) {
            //数量减少1
            shoppingCart.setNumber(number - 1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //只有一个，删除这个菜品或者套餐
            shoppingCartMapper.delete(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }
}
